package DataBaseUtils

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
	"goGrpc/helloService/Configs"
	"log"
)

var configs = Configs.LoadConfig()
var MySqlConnection *sqlx.DB

//type mySqlWorkConnect struct {
//	Conn *sqlx.DB
//}
func NewMySqlConnect() {
	dataSourceName := fmt.Sprintf("%v:%v@tcp(%v)/%v", configs.GetString("mysqlDataBase.userName"), configs.GetString("mysqlDataBase.userPassword"), configs.GetString("mysqlDataBase.address"), configs.GetString("mysqlDataBase.dataBase"))
	log.Println(dataSourceName, "配置情况")
	dataSourceName = dataSourceName + "?parseTime=true&loc=Asia%2FShanghai&charset=utf8"
	log.Println(dataSourceName, "配置情况")
	db, err := sqlx.Connect("mysql", dataSourceName)
	if err != nil {
		log.Fatalln(err)
	}
	db.SetMaxOpenConns(200)
	db.SetMaxIdleConns(10)
	MySqlConnection = db
}
